//
//  StudyListVC.m
//  students
//
//  Created by 任辰东 on 2024/2/23.
//

#import "StudyListVC.h"
#import "StudyTCell.h"
#import "RCDEmptyView.h"
#import "StudyDetailVC.h"
#import "StudyApis.h"
#import "StudyVideoDetailVC.h"

@interface StudyListVC ()

@end

@implementation StudyListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"学习";
    
    [self setupEstimatedRowHeight:200 cellClasses:@[[StudyTCell class]]];
    [self beginRefresh];
}

-(void)requestDataWithOffset:(NSInteger)offset success:(void (^)(NSArray *))success failure:(void (^)(NSString *))failure
{
    NSDictionary *params = @{
        @"class_id":[GVUserDefaults standardUserDefaults].class_id,
        @"page":@(offset),
        @"pagesize":@(10)
    };
    
    WeakSelf
    [self showLoadingHUDWithMessage:nil];
    [StudyApis studentArticleListRequestWithParams:params SuccessBlock:^(NSNumber *code, NSString *msg, id responseData) {
        StrongSelf
        [self hideLoadingHUD];
        DLog(@"responseData === %@",responseData);
        NSMutableArray *data = [StudyModel mj_objectArrayWithKeyValuesArray:responseData];
//        [data enumerateObjectsUsingBlock:^(StudyModel * obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            obj.isRecord = NO;
//        }];
        success(data);
        [self addEmptyView];
        
    } FailureBlock:^(NSNumber *code, NSString *msg) {
    StrongSelf
        [self showWarningHUDWithMessage:msg completion:NULL];
    }];
   
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    StudyModel *model;
    if (indexPath.row < self.dataSourceArr.count) {
        model = self.dataSourceArr[indexPath.row];
    }
    
    WeakSelf
    //1文章2视频
    if (model.type_id.integerValue == 1) {
        
        StudyDetailVC *detailVC = [[StudyDetailVC alloc]initWithModel:model];
        [self.navigationController pushViewController:detailVC animated:YES];
        
//        [detailVC setSuccessBlcok:^(StudyModel * _Nonnull smodel) {
//                 StrongSelf
//            [self.dataSourceArr replaceObjectAtIndex:indexPath.row withObject:smodel];
//            [self.myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
//        }];
        
    }else{
        StudyVideoDetailVC *detailVC = [[StudyVideoDetailVC alloc]initWithModel:model];
        [self.navigationController pushViewController:detailVC animated:YES];
        
//        [detailVC setSuccessBlcok:^(StudyModel * _Nonnull smodel) {
//                 StrongSelf
//            [self.dataSourceArr replaceObjectAtIndex:indexPath.row withObject:smodel];
//            [self.myTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
//        }];
    }
}

-(void)addEmptyView
{
    if (IsEmpty(self.dataSourceArr)) {
        [RCDEmptyView addEmptyWithStyle:EmptyViewStyleCommon onView:self.myTableView position:EmptyViewPositionCenter clickCallBack:nil];
    }else{
        [RCDEmptyView removeFromParentView:self.myTableView];
    }
}

@end
